package com.yz.antlr.VisitorDemo;

import java.util.List;

/**
 * @author yanzhao
 * @version 1.0
 * TODO
 * @date 2022/4/2 18:02
 */
public class MenuVisitor implements Visitor {

    @Override
    public void visit(Element e) {
        if (Menu.class.isAssignableFrom(e.getClass())) {
            Menu item = (Menu) e;
            System.out.println("{菜单名=" + item.getName() + ", 描述=" + item.getDescription() + "}");
            // 遍历子节点
            List<Component> menuComponents = item.getMenuComponents();
            for (Component component : menuComponents) {
                if (Menu.class.isAssignableFrom(component.getClass())) {
                    component.accept(this);
                } else {
                    component.accept(new ItemVisitor());
                }
            }
        }
    }
}
